package cn.com.darly.videoandroidpm.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.multidex.MultiDexApplication;
import android.telephony.TelephonyManager;

import com.darly.common.Common;
import com.darly.common.DLog;
import com.darly.dview.DView;

import cn.com.darly.videoandroidpm.BuildConfig;
import cn.com.darly.videoandroidpm.common.SystemLoginInfo;
import cn.com.darly.videoandroidpm.common.retrofit.RetrofitCfg;
import cn.com.darly.videoandroidpm.common.sugar.SugarContext;
import cn.com.darly.videoandroidpm.ui.service.MqttServiceImplementer;

/**项目启动加载初始化类
 * @author Darly/张宇辉/2018/3/5 8:47
 * @version 1.0/cn.com.darly.videoandroidpm.base
 */
public class BaseApplication  extends MultiDexApplication implements Application.ActivityLifecycleCallbacks{

    private static BaseApplication instance;

    private int activityNub;

    @Override
    public void onCreate() {
        instance = this;
        super.onCreate();
        //1.初始化緩存
        SystemLoginInfo.init(this);
        //2.初始化數據庫
        SugarContext.init(this);
        //3.初始化其他部件
        DView.init().notifyInit(BuildConfig.DEBUG, this);//初始化自定义控件集合
        Common.init().init(this, "darlyview_share");//初始化工具类中的缓存集合
        Common.init().initDlog(BuildConfig.DEBUG, "View");//初始化工具类中的日志
        Common.init().initRetrofit(getVersionCode(), BaseCons.getRequestUrl(), new RetrofitCfg());//初始化工具类中的网络请求模块
        //4.启动服务
        startService(new Intent(this, MqttServiceImplementer.class));
        registerActivityLifecycleCallbacks(this);//注册管理Activity
    }



    public static BaseApplication getInstance() {
        return instance;
    }

    /*
        获取项目的版本号
     */
    public int getVersionCode() {
        int version = 0;
        try {
            PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
            version = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    public String getDevId() {
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
        SugarContext.terminate();
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (activityNub == 0) { //后台切换到前台
            DLog.v("danxx", ">>>>>>>>>>>>>>>>>>>App切到前台");
        }
        activityNub++;
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        activityNub--;
        if (activityNub == 0) { //前台切换到后台
            DLog.v("danxx", ">>>>>>>>>>>>>>>>>>>App切到后台");
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}